% by clq ylx
% 2021/6/12
%% added timing function 
%% passed test
function[best_is_pref,opt_profits_pref]=prop_pref(j_all,pref)
global I J P
addpath functions;
Line = j_all;
N = size(Line,2); % equivalent to size(2)
genPoisson(N);
S_pref = zeros(I, J);
% Profit = zeros(patient_number, I); % For What? 
best_is_pref = zeros(1, N);
opt_profits_pref = zeros(1,N);
Q = [ones(I,1),zeros(I,N)];
R = [ones(I,1),zeros(I,N)];
for n = 1:N
    j = Line(n);
    profits = zeros(1, I);
    for i = 1:I
        Q_test = findQ2(Q,i,j,P,n); 
        R_test = findR2(Q_test,i,R,n);
        profits(i) = function_fQR2(Q_test,R_test,n);
    end
%above is the same with proposed main without preference    
%% special part for proposed pref
    pref_temp = pref(n);%randomly deciding the preference
    if pref_temp == 1
        profit_temp = max(profits(1:I/2));
        i = find(profits == profit_temp);
    else
        profit_temp = max(profits(I/2+1:end));
        i = find(profits == profit_temp);
    end               %assign to the best slot according to preference
    best_is_pref(n) = i;
    S_pref(i, j) = S_pref(i, j) + 1;
    Q = findQ2(Q,i,j,P,n);
    R = findR2(Q,i,R,n);
    opt_profits_pref(n) = profits(i);%find the optimal profit
    % fprintf("    Time = %.2f seconds \n\n", ftimer()-start_time);
end
    
% plot(1:patient_number,sum_profits);
%showTimer();